package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    /*
    作用：新增套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //1.获取登录者，补全信息
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        setmealService.save(setmealDto);
        return R.success("保存成功");
    }


    /*
    分页查询
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,String name){
        R<Page<SetmealDto>> result = setmealService.findByPage(page,pageSize,name);
        return result;
    }


    /*
    批量删除套餐
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delectByIds(ids);
        return R.success("删除成功");
    }


    /*
    修改套餐售卖状态
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatusById(@RequestParam List<Long> ids,@PathVariable Integer status){
        setmealService.updateStatusById(ids,status);
        return R.success("修改售卖状态成功");
    }


    /*
    移动端查询套餐信息
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        List<Setmeal> setmealList = setmealService.list(categoryId,status);
        return R.success(setmealList);
    }
}






























